package com.xiyin.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xiyin.config.WxAppletConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author: xukaihang
 * @description: 获取微信小程序的Scheme
 * @date: 2024/1/20 10:56
 */
@Slf4j
@RestController
@RequestMapping("/weixin")
public class UrlSchemeController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private WxAppletConfig wxAppletConfig;
    @RequestMapping("/urlScheme")
    public String getUrlScheme() {
        String accessToken = null;

        String tokenUrl = wxAppletConfig.getTokenUrl() + wxAppletConfig.getAppId() + "&secret=" + wxAppletConfig.getSecret();
        ResponseEntity<String> response = restTemplate.exchange(tokenUrl, HttpMethod.GET, null, String.class);

        String response2 = null;
        if (response.getStatusCode() == HttpStatus.OK) {
          log.info("token获取成功:"+response.getBody());
            // 输出结果
            JSONObject jsonObject = JSONUtil.parseObj(response.getBody());
            accessToken = jsonObject.get("access_token").toString();

            response2 = getStringResponseEntity(accessToken);
        } else {
           log.info("获取token失败! No access token");
        }

        return response2;
    }
    /**
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     * @Author xukaihang
     * @Description //获取微信小程序的Scheme
     * @Date 2024/1/20 16:16
     * @Param [accessToken]
     **/
    private String getStringResponseEntity(String accessToken) {
        ResponseEntity<String> response2;
        String schemeUrl = wxAppletConfig.getSchemeUrl() + accessToken;
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("path", "");
        jsonBody.put("query", "");
        jsonBody.put("env_version", "");
        JSONObject params = new JSONObject();
        params.put("jump_wxa", jsonBody);
        params.put("expire_time", "");
        params.put("expire_type", "");
        params.put("expire_interval", "");
        // 设置请求头信息（可选）
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        response2 = restTemplate.exchange(schemeUrl, HttpMethod.POST, new HttpEntity<>(params.toString(), headers), String.class);
        JSONObject jsonObject2 = null;
        String openlink = null;
        if (response2.getStatusCode() == HttpStatus.OK) {
            log.info("scheme获取成功:" + response2.getBody());
            jsonObject2 = JSONUtil.parseObj(response2.getBody());
            openlink = jsonObject2.get("openlink").toString();
            System.out.println(openlink);

        } else {
            log.info("scheme获取失败");
        }
        return openlink;
    }

}